﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AppFx.Interpretation;
using Antlr.Runtime;
using Antlr.Runtime.Tree;
using System.Diagnostics;

namespace AntlrTree
{
	public partial class TreeViewer : Form
	{
		public TreeViewer()
		{
			InitializeComponent();
		}

		private void buttonX1_Click(object sender, EventArgs e)
		{
			treeGX1.Nodes[0].Nodes.Clear();
			FilterStringLexer lexer = new FilterStringLexer(new Antlr.Runtime.ANTLRStringStream(maskedTextBoxAdv1.Text));
			CommonTokenStream tokenStream = new CommonTokenStream(lexer);
			FilterStringParser parser = new FilterStringParser(tokenStream);

			CommonTree tree = parser.expression().Tree;
			DevComponents.Tree.Node node = new DevComponents.Tree.Node() { Text = tree.Text != null ? tree.Text : String.Empty };
			//node.Cells.Add(new DevComponents.Tree.Cell(String.Format(" |T: {0}", tree.Type.ToString())));
			AddNodes(node, tree);
			treeGX1.Nodes[0].Nodes.Add(node);
			treeGX1.Nodes[0].ExpandAll();
		}

		private void AddNodes(DevComponents.Tree.Node node, CommonTree tree)
		{
			if (node != null && tree != null && tree.Children != null)
			{
				foreach (var iTree in tree.Children)
				{
					CommonTree cTree = iTree as CommonTree;
					if (cTree != null)
					{
						DevComponents.Tree.Node cNode = new DevComponents.Tree.Node() { Text = cTree.Text != null ? cTree.Text : String.Empty };
						//cNode.Cells.Add(new DevComponents.Tree.Cell(String.Format(" |T: {0}", cTree.Type.ToString())));
						node.Nodes.Add(cNode);
						AddNodes(cNode, cTree);
					}
				}
			}
		}
	}
}
